//
//  NewFriendsViewController.m
//  ALearningWalk
//
//  Created by wudongyang on 16/6/30.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "NewFriendsViewController.h"
#import "CenterNewFriendTableCell.h"
#import "AppDelegate.h"
#import "NewFriendsModel.h"

static NSString *const newFriendReuse = @"newFriendReuse";
@interface NewFriendsViewController ()

@property (nonatomic, retain) UITableView *myTableView;

@end

@implementation NewFriendsViewController
- (void)dealloc
{
    [_friendNewArrayM release];
    [_friendAllDicM release];
    [_myTableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.friendAllDicM = [NSMutableDictionary dictionary];
    self.friendNewArrayM = [NSMutableArray array];
    [self addNewFriendMassege];
    
    //创建TableView
    [self createView];

    // Do any additional setup after loading the view.
}
- (void)addNewFriendMassege
{
    // AppDelegate最大的单利
    AppDelegate *temp = [UIApplication sharedApplication].delegate;
    // 当前页好友申请字典 接收 AppDelegate的好友申请字典
    NSMutableDictionary *friendNewDicM = [NSMutableDictionary dictionaryWithDictionary:temp.buddy];
    NSLog(@"%@",temp.buddy);
    // 遍历这个请求加好友的字典 用字符串key接收
    for (NSString *key in friendNewDicM) {
        // 初始化新好友Model
        NewFriendsModel *newFriendModel = [[NewFriendsModel alloc] init];
        // 请求好友名字
        newFriendModel.userName = key;
        // 好友请求信息是名字key的value值
        newFriendModel.massege = [friendNewDicM objectForKey:key];
        // 将请求好友model存入请求好友数组
        [self.friendNewArrayM addObject:newFriendModel];
        
        // 释放请求好友model
        [newFriendModel release];
    }
    
    
    
}


- (void)createView {
    // 创建tableView
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    // xib注册
    UINib *nib = [UINib nibWithNibName:@"CenterNewFriendTableCell" bundle:[NSBundle mainBundle]];
    [self.myTableView registerNib:nib forCellReuseIdentifier:newFriendReuse];
    
    [_myTableView release];
    
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
//    view.backgroundColor = DSRGBA(140, 140, 140, 0.2);
//    [self.view addSubview:view];
//    [view release];
//    
//    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    backButton.frame = CGRectMake(25, 30, 25, 25);
//    [backButton setBackgroundImage:[UIImage imageNamed:@"camera_edit_cross"] forState:UIControlStateNormal];
//    [self.view addSubview:backButton];
//    [backButton addTarget:self action:@selector(backbuttonAction) forControlEvents:UIControlEventTouchUpInside];
//    
//    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - 50, 20, 100, 44)];
//    title.text = @"111";
//    title.textAlignment = NSTextAlignmentCenter;
//    [self.view addSubview:title];
//    [title release];
    
}

//返回按钮
- (void)backbuttonAction{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark 协议方法

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.friendNewArrayM.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CenterNewFriendTableCell *cell = [tableView dequeueReusableCellWithIdentifier:newFriendReuse];
    NewFriendsModel *model = [self.friendNewArrayM objectAtIndex:indexPath.row];
    cell.myModel = model;
    
    cell.myBlock = ^(BOOL isSuccess, NSString *userName){
        if (isSuccess) {
            AppDelegate *temp = [[UIApplication sharedApplication] delegate];
            [temp.buddy removeObjectForKey:userName];
            
            [self.friendNewArrayM removeObject:model];
        }
        [self.myTableView reloadData];
        
    };
    
    return cell;
}



@end
